/**   
 * @Title: CommandUtil.java 
 * @Package: com.rename.icode.utils 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-3-19 下午6:24:42 
 * @version  
 */

package com.rename.icode.utils;

import android.util.Log;

import com.rename.icode.comm.Constants;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-3-19 下午6:24:42
 * @version V1.0
 */

public class CommandUtil {

	private static final String TAG = "CommandUtil";

	/**
	 * 
	 * @Description 执行命令
	 * @author Administrator
	 * @param commands
	 * @return
	 */
	public static List<String> excuteCommand(String[] commands) {
		List<String> results = null;
		Process process = null;
		DataOutputStream os = null;
		try {
			LogUtil.i(TAG, "准备获取管理员权限...");
			process = Runtime.getRuntime().exec("su");
//			StreamGobblerThread errGobblerThread = new StreamGobblerThread(process.getErrorStream(), "ERROR");
//			errGobblerThread.start();
//			StreamGobblerThread outGobblerThread = new StreamGobblerThread(process.getInputStream(), "STDOUT");
//			outGobblerThread.start();
			os = new DataOutputStream(process.getOutputStream());
			LogUtil.i(TAG, "获得管理员权限...");
			for (String command : commands) {
				os.writeBytes(command + "\n");
				LogUtil.i(TAG, "command is " + command);
			}
			os.writeBytes(Constants.CMD_EXIT + "\n");
			os.flush();
			process.waitFor();
//			process.wait(10 * 1000);// 等待10秒
			LogUtil.i(TAG, "命令执行完毕...");
			BufferedReader in = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String line;
			results = new ArrayList<String>();
			while ((line = in.readLine()) != null) {
				LogUtil.i(TAG, "结果：" + line);
				results.add(line);
			}

		} catch (Exception e) {
			Log.d("debug",
					"Unexpected error - Here is what I know: " + e.getMessage());
			LogUtil.i(TAG, "err is " + e.getMessage());

		} finally {
			try {
				if (os != null) {
					os.close();
				}
				if (process != null) {
					process.destroy();
					process = null;
					LogUtil.i(TAG, "进程销毁");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return results;
	}

	private static class StreamGobblerThread extends Thread {
		InputStream is;
		String type;
		OutputStream os;

		StreamGobblerThread(InputStream is, String type) {
			this(is, type, null);
		}

		StreamGobblerThread(InputStream is, String type, OutputStream redirect) {
			this.is = is;
			this.type = type;
			this.os = redirect;
		}

		@Override
		public void run() {
			InputStreamReader isr = null;
			BufferedReader br = null;
			PrintWriter pw = null;
			try {
				if (os != null) {
					pw = new PrintWriter(os);
				}
				isr = new InputStreamReader(is);
				br = new BufferedReader(isr);
				String line = null;
				while ((line = br.readLine()) != null) {
					if (pw != null) {
						pw.println(line);
					}
					LogUtil.i(TAG, type + ">" + line);
				}

				if (pw != null) {
					pw.flush();
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} finally {// 关闭流
				if (null != pw) {
					pw.close();
					pw = null;
				}
				if (null != br) {
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					br = null;
				}
				if (null != isr) {
					try {
						isr.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					isr = null;
				}
			}
		}

	}

}
